package com.example.jksj.bookmanagement.pro.home.fragment;
/*
 *  项目名：    SharedRefrigerators
 *  包名：      com.sr.sharedrefrigerators.fragment
 *  文件名:     HomeFragment
 *  创建者:     HXD
 *  创建时间:   2018/11/25 12:13
 *  描述:       TODO
 */


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.pro.home.adapter.BookAdapter;
import com.example.jksj.bookmanagement.utils.GlideImageLoader;
import com.example.jksj.bookmanagement.bean.BookBean;
import com.example.jksj.bookmanagement.common.MyLazyFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;

public class HomeFragment extends MyLazyFragment {
    private ArrayList<String> imageUrlList;
    private ArrayList<BookBean> recommendBookList;
    private ArrayList<BookBean> hotBookList;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected int getTitleBarId() {
        return 0;
    }

    @Override
    public void initView() {
        setStatusBar();
        setBanner();
        recyclerView1 = getView().findViewById(R.id.recyclerView);   // 好书推荐
        recyclerView2 = getView().findViewById(R.id.recyclerView2);  // 热门图书
        setRecommendBooks();
        setHotBooks();

        //设置适配器
        recyclerView1.setAdapter(new BookAdapter(recommendBookList));
        recyclerView2.setAdapter(new BookAdapter(hotBookList));
        //构造 LinearLayputManager，并设置方向。LinearLayputManager效果类似ListView。
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getContext());

        //设置滑动方向：纵向
//      layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //添加Android自带的分割线
//      recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        //设置滑动方向：横向
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView1.setLayoutManager(layoutManager);
        recyclerView2.setLayoutManager(layoutManager2);

        // 设置存在 editView 的 Layout 的监听事件
        final LinearLayout topLayout = getView().findViewById(R.id.topLayout);
        topLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                topLayout.requestFocus();
                return false;
            }
        });
    }

    @Override
    public void initData() {

    }

    private void setStatusBar(){
        int result = 0;
        View statusBarView = getView().findViewById(R.id.statusBat_bg);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) statusBarView.getLayoutParams();
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = this.getResources().getDimensionPixelSize(resourceId);
        }

        linearParams.height = result;

        statusBarView.setLayoutParams(linearParams);

    }

    public void setBanner(){
        imageUrlList = new ArrayList<>();
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2018/12/17/49401.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2018/11/29/48767.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2018/11/19/48365.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2018/04/09/37820.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2017/06/21/29534.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2006/11/12/25023.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2017/06/21/29533.jpg");
        imageUrlList.add("http://www.huse.edu.cn//upload/resources/image/2006/11/12/25006.jpg");
        Banner banner = (Banner) getView().findViewById(R.id.banner);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(imageUrlList);
        banner.setDelayTime(5000);
        banner.start();

    }

    public void setRecommendBooks(){
        recommendBookList = new ArrayList<BookBean>(24);
        recommendBookList.add(new BookBean("莫失莫忘","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1537322934.jpg"));
        recommendBookList.add(new BookBean("十一种孤独","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512707236.jpg"));
        recommendBookList.add(new BookBean("火星纪事","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512707205.jpg"));
        recommendBookList.add(new BookBean("双重赔偿","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512706913.jpg"));
        recommendBookList.add(new BookBean("流动的盛宴","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512706668.jpg"));
    }

    public void setHotBooks(){
        hotBookList = new ArrayList<BookBean>(24);
        hotBookList.add(new BookBean("莫失莫忘","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1537322934.jpg"));
        hotBookList.add(new BookBean("十一种孤独","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512707236.jpg"));
        hotBookList.add(new BookBean("火星纪事","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512707205.jpg"));
        hotBookList.add(new BookBean("双重赔偿","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512706913.jpg"));
        hotBookList.add(new BookBean("流动的盛宴","http://e.library.sh.cn/wread2017/_cover/shyw/ShlibEpub_1512706668.jpg"));
    }
}
